package gongshw.bank.servlets;

import gongshw.bank.model.User;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class StockListServlet
 */
@WebServlet("/Balance")
public class BalanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BalanceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}
	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
	throws IOException, ServletException {
		HttpSession session = req.getSession(true);
		ServletContext context = getServletContext();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			resp.sendRedirect("/j2eeBank/user/login.html");
		} else if(user.isInDebt()){
			context.getRequestDispatcher("/bank/warning.jsp").forward(req, resp);
		} else {
			context.getRequestDispatcher("/bank/money.jsp").forward(req, resp);
		}
}

}
